<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--用户列表卡片-->
    <el-card class="box-card">
      <el-form :inline="true" :model="goods" class="demo-form-inline" label-width="70px">
        <el-form-item label="类别" label-width="70px">
          <el-cascader
            placeholder="请选择商品类别"
            :change-on-select="true"
            @change="selectChange"
            v-model="categorykeys"
            :props="searchSelectProps"
            :options="cateories"
            clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="名称" label-width="70px">
          <el-input clearable v-model="goods.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 30px">
          <el-button icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="getGoods">查询</el-button>
          <el-button type="success" icon="el-icon-plus">添加</el-button>
        </el-form-item>
      </el-form>
      <!--      表格内容显示区域-->
      <el-table
        v-loading="loading"
        :data="goodsList"
        border
        max-height="600"
        style="width: 100%;">
        <el-table-column
          prop="id"
          label="#"
          width="50">
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          width="200">
        </el-table-column>
        <el-table-column prop="model"  label="规格型号" width="100">
          <template slot-scope="scope">
            <el-tag size="small" effect="plain" type="success">
              {{ scope.row.model }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="unit"
          label="单位"
          width="70">
        </el-table-column>
        <el-table-column
          prop="remark"
          label="描述"
          width="700">
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          width="120">
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding-top:15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 12, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getCatList } from '../../api/category'
import { getGoodsList } from '../../api/goods'

export default {
  name: 'Goods',
  data () {
    return {
      //当前页
      current: 1,
      size: 6,
      total: 0,
      loading: true,
      categorys:'',
      goods: {
        name: '',
        model:'',
        unit:''
      },
      categorykeys: [],//搜索类别
      cateories: [], //类别选择
      searchSelectProps: {
        expandTrigger: "hover",
        value: "id",
        label: "name",
        children: "children",
        checkStrictly: true
      },
      goodsList:[],
      // 角色对话框的可见性
    }
  },
  created () {
    //创建组件的时候调用查询所有用户的方法
    this.getGoods()
    this.getCatetorys();
    setTimeout(() => {
      this.loading = false
    }, 500)
  },
  methods: {
    // 每页条数改变
    handleSizeChange (val) {
      this.size = val
      this.getGoods()
      // console.log(`每页 ${val} 条`);
    },
    // 当前页码改变
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`);
      this.current = val
      this.getGoods()
    },
    /**
     * 加载物资类别
     */
    async getCatetorys() {
      const res= await getCatList()
      if (res.code!=200) {
        return this.$message.error("获取物资类别失败");
      } else {
        this.cateories = res.data.cat;
      }
    },
    /**
     * 加载商品列表
     */
    async getGoods() {
      const res= await getGoodsList(this.current,this.size,this.categorys,this.goods)
      if (res.code!=200) {
        return this.$message.error("获取商品列表失败");
      } else {
        this.goodsList = res.data.goodsList;
        this.total = res.data.total
      }
    },
    /**
     * 分类搜索改变时
     */
    selectChange() {
      var str = "";
      for (var i = 0; i < this.categorykeys.length; i++) {
        str += this.categorykeys[i] + ",";
      }
      str = str.substring(0, str.length - 1);
      this.categorys = str;
    }
  }
}
</script>

<style scoped>

</style>
